<?php
namespace app\admin\controller;

use think\Controller;
use think\Session;
use think\Request;
use app\admin\model\AdminUserModel;

class Adminuser extends Common
{
	/**
	*	后台管理员列表
	*/
	public function index()
	{
		$data = input('post.');
		$AdminUserModel = new AdminUserModel();
		$list = $AdminUserModel->getAdminuser($data);
		if(!empty($list)){
			// 获取分页显示
	        $page = $list->render();
			$this->assign('list',$list);
			$this->assign('page', $page);
		}
		$this->assign('username',isset($data['username']) ? $data['username'] : '');
		$this->assign('status',isset($data['status']) ? $data['status'] : '');
		return $this->fetch('/adminuser/index');
	}

	/**
	*	后台添加管理员
	*/
	public function add()
	{
		return $this->fetch('/adminuser/add');
	}

	/**
	*	后台添加管理员提交
	*/
	public function addPost()
	{

		if ($this->request->isPost()) {
			$data = input('post.');

			$result = $this->validate($data, 'AdminUserValidate');

	        if ($result !== true) {

	            $this->error($result);
	        }

			$AdminUserModel = new AdminUserModel();
			$list = $AdminUserModel->addAdminUser($data);
			if ($list === 2) {
				$this->error('该用户名已存在');
			}elseif ($list){
	        	$this->success('添加成功');
	        }else{
	        	$this->error('添加失败');
	        }
		}
	}

	/**
	*	后台修改管理员信息
	*/
	public function edit()
	{
		$data = $this->request->param();
		$AdminUserModel = new AdminUserModel();
		$list = $AdminUserModel->getOne($data['id']);
		if($list){
			$this->assign('list',$list);
		}
		return $this->fetch('/adminuser/edit');
	}

	/**
	*	后台修改管理员信息提交
	*/
	public function editPost()
	{
		
		if ($this->request->isPost()) {
			$data = $this->request->param();

			$rule = [
		        'username' => 'require'
		    ];

		    $message = [
		        'username.require' => '用户名不能为空'
		    ];

			$result = $this->validate($data,$rule,$message);
			
	        if ($result !== true) {

	            $this->error($result);
	        }

			$AdminUserModel = new AdminUserModel();
			$list = $AdminUserModel->editAdminUser($data['id'],$data);

	        $this->success('修改成功',url('/admin/adminuser/index'));
		}
	}

	/**
	*	后台删除管理员信息
	*/
	public function delete()
	{
		$data = $this->request->param();
		$AdminUserModel = new AdminUserModel();
		$list = $AdminUserModel->deleteAdminUser($data['id']);
		if ($list){
	    	$this->success('删除成功',url('/admin/adminuser/index'));
	    }else{
	    	$this->error('删除失败');
	    }
	}

	/**
	*	后台禁用管理员
	*/
	public function ban()
	{
		$data = $this->request->param();
		$AdminUserModel = new AdminUserModel();
		$list = $AdminUserModel->banAdminUser($data['id']);
		if ($list){
	    	$this->success('禁用成功',url('/admin/adminuser/index'));
	    }else{
	    	$this->error('禁用失败');
	    }
	}

	/**
	*	启用管理员
	*/
	public function cancelBan()
	{
		$data = $this->request->param();
		$AdminUserModel = new AdminUserModel();
		$list = $AdminUserModel->cancelBanAdminUser($data['id']);
		if ($list){
	    	$this->success('启用成功',url('/admin/adminuser/index'));
	    }else{
	    	$this->error('启用失败');
	    }
	}
}